package com.beiing.tianshuai.tianshuai.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.base.BaseRecyclerViewAdapter;
import com.beiing.tianshuai.tianshuai.entity.PopupWindowListBean;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * popupWindow选择的recycleView 的适配器
 */
public class PopupWindowSelectedAdapter extends BaseRecyclerViewAdapter<PopupWindowListBean> {

    private Context mContext;

    private OnItemClickListener mOnItemClickListener;

    public PopupWindowSelectedAdapter(Context context) {
        this.mContext = context;
    }

    @Override
    public CommonViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflate = View.inflate(mContext, R.layout.item_rv_popup_list_selected, null);
        return new CommonViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        PopupWindowListBean listBean = mList.get(position);
        if (holder instanceof CommonViewHolder) {
            ((CommonViewHolder) holder).setData(listBean);
            ((CommonViewHolder) holder).setListener(listBean);
        }
    }

    public class CommonViewHolder extends RecyclerView.ViewHolder {

        /**
         * item文字
         */
        @BindView(R.id.tv_item_text)
        TextView tvItemText;
        /**
         * item选项图片
         */
        @BindView(R.id.ib_selected)
        ImageButton ibSelected;

        CommonViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        private void setData(PopupWindowListBean bean) {
            tvItemText.setText(bean.getItemText());
            //设置选项
            ibSelected.setImageDrawable(bean.isSelected() ?
                    ContextCompat.getDrawable(mContext, R.drawable.ic_radio_on_blue) :
                    ContextCompat.getDrawable(mContext, R.drawable.ic_radio_off_gray)
            );
        }

        private void setListener(final PopupWindowListBean bean) {
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnItemClickListener != null) {
                        mOnItemClickListener.onItemSelected(getAdapterPosition(), bean);
                    }
                }
            });
        }
    }

    public interface OnItemClickListener {
        void onItemSelected(int position, PopupWindowListBean bean);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }
}
